package com.ccsc.auth.controller;

import com.ccsc.auth.entity.ShiroSysUser;
import com.ccsc.auth.entity.ShiroUser;
import com.ccsc.auth.utils.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.WebRequest;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Locale;

/**
 * Created by houbo on 2016/3/2.
 */
public class BaseController {
    @Autowired
    private MessageSource messageSource;

    public DataMap getParam(WebRequest request) {
        Iterator<String> it = request.getParameterNames();
        DataMap result = new DataMap();
        while (it.hasNext()) {
            String key = it.next();
            result.put(key, request.getParameter(key));
        }
        return result;
    }

    public BaseResponse ok(Object data) {
        return new BaseResponse(HttpStatus.SUCCESS, data);
    }

    public BaseResponse ok(int code, String msg) {
        return new BaseResponse(HttpStatus.SUCCESS, msg, true);
    }

    public BaseResponse error(int code, Object data) {
        return new BaseResponse(code, data);
    }

    public BaseResponse error(int code, String msg) {
        return new BaseResponse(code, msg, false);
    }

    public BaseResponse error(int code) {
        BaseResponse baseResponse = null;
        switch(code){
            case HttpStatus.NOTFOUND: baseResponse = new BaseResponse(code, getMessageSource(String.valueOf(HttpStatus.NOTFOUND)));break;
            case HttpStatus.EXCEPTION: baseResponse = new BaseResponse(code, getMessageSource(String.valueOf(HttpStatus.EXCEPTION)));break;
            case HttpStatus.FORBIDDEN: baseResponse = new BaseResponse(code, getMessageSource(String.valueOf(HttpStatus.FORBIDDEN)));break;
            default: baseResponse = new BaseResponse(code, getMessageSource(String.valueOf(HttpStatus.EXCEPTION)));break;
        }
        return baseResponse;
    }

    public String getMessageSource(String code) {
        return messageSource.getMessage(code, new Object[] {}, Locale.SIMPLIFIED_CHINESE);
    }

    public void setDefaultPage(DataMap params) {
        if(params.get("page") == null){
            params.put("page", 1);
        }
        if(params.get("pageSize") == null){
            params.put("pageSize", 10);
        }
    }

    public Boolean hasPage(DataMap params) {
        if(params.get("page") != null && params.get("pageSize") != null){
            return true;
        }
        return false;
    }
    public void refreshPage(Page page){
        page.setTotalRecord(page.getContent().size());
        page.setTotalPageCount((page.getTotalRecord()  +  page.getPageSize()  - 1) / page.getPageSize());
    }

    public Session getSession() {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        return session;
    }

    public ShiroUser getSessionUser() {
        Session session = getSession();
        ShiroUser shiroUser = new ShiroUser();
        ShiroSysUser shiroSysUser = session != null ? (ShiroSysUser) session.getAttribute("loginSysUser") : null;
        if(shiroSysUser!=null && shiroSysUser.getUserId()!=null){
            shiroUser = SSOResourceUtil.getUserById(String.valueOf(shiroSysUser.getUserId()));
            session.setAttribute("loginUser", shiroUser);
        }
        return shiroUser;
    }

    public ServletContext getApplication(){
        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        ServletContext application = webApplicationContext.getServletContext();
        return application;
    }

    @ExceptionHandler
    public BaseResponse expHandler(HttpServletRequest request, Exception ex) {
        ex.printStackTrace();
        return this.error(500, getMessageSource("500")+":"+ex.getMessage());
    }
}
